#ifndef SESSIONFRIENDAREA_H
#define SESSIONFRIENDAREA_H

#include <QWidget>

class SessionFriendItem;
class QScrollArea;
class QLabel;

////// 滚动区域中的Item类型
enum ItemType {
    SessionItemType,
    FriendItemType,
    ApplyItemType
};

class SessionFriendArea : public QWidget
{
    Q_OBJECT
public:
    explicit SessionFriendArea();

    void clear();

    // 根据item类型和该类型对应的ID, 将元素添加到container的layout中
    void addItem(ItemType itemType, const QString& id, const QIcon& avatar,
                 const QString& name, const QString& description);
    void clickItem(int idx);

    QList<SessionFriendItem*>& getSelectItems();

    // 滚动条置顶
    void scrollToBeginLater();

private:
    QScrollArea* scrollArea;
    // 往container内部的layout添加元素，当container高度超过QScrollArea, 触发滚动效果
    QWidget* container;
    QList<SessionFriendItem*> selectItems;

};


class SessionFriendItem : public QWidget
{
    Q_OBJECT
public:
    SessionFriendItem(QWidget* owner, const QIcon& avatar, const QString& name, const QString& lastMessage);
    void select();
    void active();

protected:
    void paintEvent(QPaintEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void enterEvent(QEnterEvent* event) override;
    void leaveEvent(QEvent* event) override;

protected:
    QLabel* messageLabel;

    using FuncPtr = void (SessionFriendItem::*)();
    FuncPtr func = nullptr;
    QWidget* owner;         // 该item所属的widget
private:
    bool selected = false;
};


////// 会话最小单元结构
class SessionItem : public SessionFriendItem
{
    Q_OBJECT
public:
    SessionItem(QWidget* owner, const QString& chatSessionId, const QIcon& avatar,
                const QString& name, const QString& lastMessage);

    QString getChatSessionId();

protected:
    void activeImpl();

private:
    void updateLastMessage(const QString& chatSessionId);

private:
    QString chatSessionId;     // 会话id
    QString lastMessage;       // 最后一条消息
};

////// 好友最小单元结构
class FriendItem : public SessionFriendItem
{
    Q_OBJECT
public:
    FriendItem(QWidget* owner, const QString& userId, const QIcon& avatar,
                const QString& name, const QString& description);

    QString getUserId();

protected:
    // virtual void active() override;
    void activeImpl();

private:
    QString userId;     // 好友id
};

////// 好友申请最小单元结构
class ApplyItem : public SessionFriendItem
{
    Q_OBJECT
public:
    ApplyItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name);

protected:
    void activeImpl();

    void onClickedByAcceptBtn();

    void onClickedByRejectBtn();

private:
    QString userId;     // 申请好友id
};


#endif // SESSIONFRIENDAREA_H
